package com.weibo.common;

import java.lang.reflect.Method;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class ConversRsTOLS {
	public static List toList(ResultSet rs,Class cls)   
	{   
	        List list = new ArrayList();   
	        try  
	        {                
	            ResultSetMetaData meta=rs.getMetaData();   
	            Object obj=null;   
	            while(rs.next())   
	            {   
	                obj=Class.forName(cls.getName()).newInstance();   
	                for(int i=1;i<=meta.getColumnCount();i++)   
	                {  
	                    String colName=meta.getColumnName(i);   
	                    colName=new String(colName.charAt(0)+"").toUpperCase()+colName.substring(1,colName.length());   
	                    String methodName="set"+colName;   
	                    Object value=rs.getObject(i);
	                    if(value==null){
	                    	value="";
	                    }
	                    
	                    try{
		                    Method method=obj.getClass().getMethod(methodName, value.getClass());   
		                    method.invoke(obj, value); 
	                    }catch(Exception e)
	                    {
	                    	//e.printStackTrace();
	                    	continue;
	                    }
	                }   
	                list.add(obj);   
	            }   
	        }catch(Exception ex)   
	        {   
	            ex.printStackTrace();   
	        }finally{   
	            return list;   
	        }   
	  }

	public static List ResultSetToList(ResultSet rs) {
	        try {
	            ResultSetMetaData md = rs.getMetaData();    // ��ý��ṹ��Ϣ���ֶ����ֶ���ȣ�
	            int columnCount = md.getColumnCount();
	            List list = new ArrayList();
	            while (rs.next()) {
	                Map rowData = new HashMap();
	                for (int i = 1; i <= columnCount; i++) {
	                    rowData.put(md.getColumnName(i), rs.getObject(i));
	                }
	                list.add(rowData);
	            }
	            return list;
	        } catch (SQLException e) {
	            e.printStackTrace();
	        }
	        return null;
	    } 



}
